//
//  WelcomeVCViewController.swift
//  我的微博
//
//  Created by gongjunna on 15/7/31.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class WelcomeVCViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
//        iconView.setImageWithURL(NSURL(string: (UserAccount.loadUserAccounts()?.avatar_large)!))
    }
    var constraint :NSLayoutConstraint?
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        
        // 待使用 其他 动画 
       UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.constraint?.constant = UIScreen.mainScreen().bounds.height - self.constraint!.constant
        
        self.view.layoutIfNeeded()
        
        }) { (_) -> Void in
            // 发送 通知
            UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
            
        }}
    func prepareUI() {
        view.addSubview(backGroundView)
        view.addSubview(iconView)
        view.addSubview(lableView)
        
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
            // 格式 “H:|-0-[view]-0-|”
       
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backGroundView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backGroundView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant:160 ))
            constraint = view.constraints.last
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        lableView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
    }

    private  lazy  var backGroundView : UIImageView =  UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView : UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.masksToBounds = true
        icon.layer.cornerRadius = 45
        icon.backgroundColor = UIColor.redColor()
        return icon
    }()
    
    private lazy  var lableView  : UILabel = {
            let lable = UILabel()
        
            lable.text = "欢迎回来"
            lable.textAlignment = NSTextAlignment.Center
            lable.sizeToFit()
        
            return lable
        
    }()
}
